var cfm = require('../models/cfm.js');
var db = require('./db');
var mongodb = new db();
var ObjectID = require("mongodb").ObjectID;
var logger = require('./logger.js');

exports.create= function createCD(filter,callback){
    mongodb.getCollection('cfm_index',function(collection){
        collection.insert(filter[0], {safe: true}, function(err, doc) {
            logger.generateLogData('INFO','cfm','insert',filter[0]);
            console.log(doc);

            return callback(err, doc);
        });
    });
};

//get正常
exports.get = function getCD(filter,callback){
    mongodb.getCollection('cfm_index',function(collection){
        collection.find(filter).toArray(function(err, docs) {
            if(err != null){
                return callback(err, docs);
            }
            if(docs === null){
                err = "not Exist";
                return callback(err, docs);
            }
            return callback(err, docs);
        });
    });
};

//测试通过
exports.remove = function removeCfmIndex(filter,callback){
    mongodb.getCollection('cfm_index',function(collection){
        collection.remove(filter,function(err){
            if (err) console.warn(err.message);
            else console.log('cfmIndex successfully removed');
            return callback();
        });
    });
};

exports.addOne = function addOne(filter){
    mongodb.getCollection('cfm_index',function(collection){
        collection.update( filter,{"$inc":{reference:1}},{safe:true},function(err,number) {
            if (err) console.warn(err.message);
            else console.log('cfm_index successfully addOne');
        });
    });
}

exports.removeOne = function removeOne(filter){
    mongodb.getCollection('cfm_index',function(collection){
        collection.update( filter,{"$inc":{reference:-1}},{safe:true},function(err,number) {
            if (err) console.warn(err.message);
            else console.log('cfm_index successfully removeOne');
        });
    });
}
